//904.水果成篮
class Solution1 {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        //统计窗口内水果的种类
       int[] hash = new int[n+1];
        
        int left = 0;
        int ret = 0;
        int kinds = 0;
        for(int right = 0;right<n;right++){
            //维护水果种类
            if(hash[fruits[right]] == 0){
                kinds++;
            }
            //进窗口
            hash[fruits[right]]++;
            
            //判断
            while(kinds > 2){
                //出窗口
                hash[fruits[left]]--;
                if(hash[fruits[left]] == 0){
                    kinds--;
                }
                left++;
            }
            //更新结果
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }



    /*
     public int totalFruit(int[] fruits) {
        int n = fruits.length;
        //统计窗口内水果的种类
       Map<Integer,Integer> hash = new HashMap<>();
        
        int left = 0;
        int ret = 0;
        
        for(int right = 0;right<n;right++){
            int in = fruits[right];
            hash.put(in,hash.getOrDefault(in,0)+1);  //进窗口
            
            //判断
            while(hash.size() > 2){
                int out = fruits[left];
                hash.put(out,hash.get(out)-1);//出窗口

                if(hash.get(out) == 0){
                   hash.remove(out);
                }
                left++;
            }
            //更新结果
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }
    */
}